// -*- C++ -*-

//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2009-05-29 00:10
 */
//========================================================================

#ifndef _GUARD_H_
#define _GUARD_H_

namespace ndk
{
  /**
   * @class guard
   *
   * @brief scoped auto lock
   */
  template<typename lock>
  class guard
  {
  public:
    guard(lock &l)
      : mutex_(l)
    {  
      this->mutex_.acquire(); 
    }
    guard(const lock &l)
      : mutex_(const_cast<lock &>(l))
    {  
      this->mutex_.acquire(); 
    }
      
    ~guard()
    {
      this->mutex_.release();
    }

    void acquire(void)
    {
      this->mutex_.acquire();
    }
      
    void release(void)
    {
      this->mutex_.release();
    }

    int try_acquire(void)
    {
      return this->mutex_.try_acquire();
    }

    lock &mutex(void)
    {
      return this->mutex_;
    }
  private:
    lock &mutex_;
  };
  
} // namespace ndk
#endif // _GUARD_H_

